require 'xml_parser'
require 'client'

class ModelClass
	attr_accessor :clients
	@@instance = nil

	def initialize()
		@clients = Hash.new
	end
	def add_client(client)
		@clients[client.name] = client
	end
	def clear
		@clients = nil
		@clients = Hash.new
	end

	def parse(node)
		clients = node.find('//client')
		clients.each() do |n|
			clt = Client.new("")
			clt.parse(n)
			@clients[clt.name] = clt
		end
	end

	def ModelClass.instance()
		if(@@instance == nil)
			@@instance = ModelClass.new()
		end
		return @@instance
	end
	
	def print()
		@clients.each() do |key, val|
			val.print()
		end
	end

	def find_client(clt)
		return @clients[clt]
	end

	def find_artist(art)
		@clients.each() do |key, val|
			if(!val.artists[art].nil?)
				return val.artists[art]
			end
		end
		return nil
	end

	def find_album(album)
		@clients.each() do |c_key, clt|
			clt.artists.each() do |a_key, art|
				if(art.albums[album].nil?)
					return art.albums[album]
				end
			end
		end
		return nil
	end

	def find_title(title)
		@clients.each() do |c_key, clt|
			clt.artists.each() do |a_key, art|
				art.albums.each() do |al_key, alb|
					if(!alb.titles[title].nil?)
						return alb.titles[title]
					end
				end
			end
		end
		return nil
	end

	def find_title_from_file(client, file)
		client.artists.each() do |a_key, art|
			art.albums.each() do |al_key, alb|
				alb.titles.each() do |t_key, title|
					if(title.file == file)
						return {"title" => title, 
							"album" => alb,
							"artist" => art, 
							"client" => client }
					end
				end
			end
		end
	end

	private :initialize
end
